home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr13 / afv95.zip / INDEX.CPP < prev    next >
C/C++ Source or Header  |  1995-02-12  |  3KB  |  207 lines

  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "index.h"
  6.  
  7. #define AFSep        "                             Directory"
  8.  
  9. static void        Clip(char *str);
  10. static void        LoadIDX(FILE *f);
  11. static void        MkAFIDX();
  12. static void        MkIDX(FILE *f);
  13. static ulong    ReadNum(char *s);
  14. static void        WriteIDX();
  15.  
  16. static ulong    bcnt[256];
  17. static char        descr[256][66];
  18. static ulong    fcnt[256];
  19. static ulong    offset[256];
  20. static uint        topcnt;
  21.  
  22. ulong    BytCnt(uint t)
  23. {
  24.     if (t >= topcnt)
  25.         return 0;
  26.     else
  27.         return bcnt[t];
  28. }
  29.  
  30. void    Clip(char *str)
  31. {
  32.     char    *s;
  33.  
  34.     s = strchr(str, 0);
  35.  
  36.     while (s > str && isspace(*(s - 1)))
  37.         *--s = 0;
  38. }
  39.  
  40. char    *Descr(uint t)
  41. {
  42.     if (t >= topcnt)
  43.         return 0;
  44.     else
  45.         return descr[t];
  46. }
  47.  
  48. ulong    FilCnt(uint t)
  49. {
  50.     if (t >= topcnt)
  51.         return 0;
  52.     else
  53.         return fcnt[t];
  54. }
  55.  
  56. void    LoadIDX(FILE *f)
  57. {
  58.     fread(&topcnt, 1, sizeof(topcnt), f);
  59.     fread(offset, topcnt, sizeof(ulong), f);
  60.     fread(descr, topcnt, sizeof(descr[0]), f);
  61.     fread(fcnt, topcnt, sizeof(ulong), f);
  62.     fread(bcnt, topcnt, sizeof(ulong), f);
  63. }
  64.  
  65. void    MkAFIDX()
  66. {
  67.     FILE    *f;
  68.  
  69.     f = fopen("allfile.dir", "rb");
  70.  
  71.     if (!f)
  72.         {
  73.         fprintf(stderr, "Unable to open ALLFILE.DIR\n");
  74.         exit(1);
  75.         }
  76.  
  77.     MkIDX(f);
  78.  
  79.     fclose(f);
  80.  
  81.     WriteIDX();
  82. }
  83.  
  84. void    MkIDX(FILE *f)
  85. {
  86.     char    line[100];
  87.  
  88.     while (!feof(f))
  89.         {
  90.         uint    t;
  91.  
  92.         while (!feof(f))
  93.             {
  94.             fgets(line, sizeof(line), f);
  95.  
  96.             if (!memcmp(line, "│ Dir ", 6))
  97.                 break;
  98.             }
  99.  
  100.         if (!feof(f))
  101.             {
  102.             if (line[6] == ' ')
  103.                 t = atoi(line + 7);
  104.             else 
  105.                 t = atoi(line + 6);
  106.  
  107.             if (t < 1 || t > 256)
  108.                 {
  109.                 fprintf(stderr, "Error: Directory %d is out of bounds.\n", t);
  110.                 exit(1);
  111.                 }
  112.             else 
  113.                 printf("Found subdirectory %u.\r", t);
  114.  
  115.             memcpy(descr[t - 1], line + 11, 65);
  116.             Clip(descr[t - 1]);
  117.  
  118.             fgets(line, sizeof(line), f);
  119.             fgets(line, sizeof(line), f);
  120.  
  121.             fcnt[t - 1] = ReadNum(line + 13);
  122.             bcnt[t - 1] = ReadNum(line + 32);
  123.  
  124.             fgets(line, sizeof(line), f);
  125.             fgets(line, sizeof(line), f);
  126.             fgets(line, sizeof(line), f);
  127.  
  128.             offset[t - 1] = ftell(f);
  129.  
  130.             if (t > topcnt)
  131.                 topcnt = t;
  132.             }
  133.         }
  134.  
  135.     printf("\n");
  136. }
  137.  
  138. ulong    Offset(uint topic)
  139. {
  140.     if (topic >= topcnt)
  141.         return 0;
  142.     else
  143.         return offset[topic];
  144. }
  145.  
  146. void    OpenIndex()
  147. {
  148.     FILE    *f;
  149.  
  150.     f = fopen("allfile.idx", "rb");
  151.  
  152.     if (f)
  153.         {
  154.         LoadIDX(f);
  155.  
  156.         fclose(f);
  157.         }
  158.     else
  159.         MkAFIDX();
  160. }
  161.  
  162. ulong    ReadNum(char *s)
  163. {
  164.     ulong    val;
  165.  
  166.     while (isspace(*s))
  167.         s++;
  168.  
  169.     val = 0;
  170.  
  171.     while (isdigit(*s) || *s == ',')
  172.         {
  173.         if (isdigit(*s))
  174.             val = val * 10 + *s - '0';
  175.  
  176.         s++;
  177.         }
  178.  
  179.     return val;
  180. }
  181.  
  182. uint    TopicCnt()
  183. {
  184.     return topcnt;
  185. }
  186.  
  187. void    WriteIDX()
  188. {
  189.     FILE    *f;
  190.  
  191.     f = fopen("allfile.idx", "wb");
  192.  
  193.     if (!f)
  194.         {
  195.         fprintf(stderr, "Unable to create ALLFILE.IDX\n");
  196.         exit(1);
  197.         }
  198.  
  199.     fwrite(&topcnt, 1, sizeof(topcnt), f);
  200.     fwrite(offset, topcnt, sizeof(ulong), f);
  201.     fwrite(descr, topcnt, sizeof(descr[0]), f);
  202.     fwrite(fcnt, topcnt, sizeof(ulong), f);
  203.     fwrite(bcnt, topcnt, sizeof(ulong), f);
  204.  
  205.     fclose(f);
  206. }
  207.